# -*- coding=utf-8 -*-
# Author: Slp
# @Date : 2021-09-15

import requests
import os
from qiniu import Auth, put_file, etag


class FileUpload:

    def staticUpload(self):
        url = "https://uc.fangdd.com/public/login/username-secret-sms"
        body = {
            "username": "sunlinpeng",
            "password": "slp900211!!",
            "smsvcode": "123456",
            "intranet-token": "a418ac2abc5eaa0023a0a5c1abeda4f235dca11d",
            "url": "https://uc.fangdd.com/oauth2/authorize?client_id=esf-staticserver&response_type=code&redirect_uri=http://static-upload-toolkit.fangdd.net/user/auth&scope=EMPLOYEE",
            "wxtoken": ""}
        r = requests.post(url, data=body)
        print(r.json())

    def qiNiu(self,fileName):
        '''
        本图图片的上传
        :param local_img: 本地图片路径
        :param img_name: 上传后的网络上保存的图片名称
        :return img_url: 远程图片的路径(绝对路径)
        '''
        #  Access Key 和 Secret Key
        access_key = 'yyxrLwnSjJ81_QPh26dBpcaH9ikTaQPY7Se8_Php'
        secret_key = 'WQNMBRAO0BYBc18tBgUFz-laobFbtE_a_eJS0kwT'
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = 'jyy-4m'
        # 指定路径
        path = r'../../testresult/'
        # 返回指定路径的文件夹名称
        dir = path+fileName
        # 循环遍历该目录下的照片

        key = fileName
        # 生成上传 Token，可以指定过期时间等
        token = q.upload_token(bucket_name, key, 3600000000)
        # 要上传文件的本地路径
        localfile = dir
        ret, info = put_file(token, key, localfile)
        print(info)
        assert ret['key'] == key
        assert ret['hash'] == etag(localfile)


if __name__ == '__main__':
    a = FileUpload()
    a.qiNiu('2021-09-14 17_20_36_result.html')
